﻿using System.Xml.Linq;

namespace EasyXaf.DynamicForm.Models;

public abstract class FormItem : Element
{
    public string Name { get; set; }

    public string Caption { get; set; }

    public string Description { get; set; }

    public bool? ShowCaption { get; set; }

    public int? Width { get; set; }

    public int? Height { get; set; }

    public Form Form { get; protected set; }

    public FormItem(Form form, Element parent)
        : base(parent)
    {
        Form = form;
    }

    public override void WriteXml(XElement element)
    {
        base.WriteXml(element);

        if (!string.IsNullOrWhiteSpace(Name))
        {
            SetXmlAttributeValue(element, "name", Name);
        }

        if (!string.IsNullOrWhiteSpace(Caption))
        {
            SetXmlAttributeValue(element, "caption", Caption);
        }

        if (!string.IsNullOrWhiteSpace(Description))
        {
            SetXmlAttributeValue(element, "description", Description);
        }

        if (ShowCaption.HasValue)
        {
            SetXmlAttributeValue(element, "show-caption", ShowCaption);
        }

        if (Width.HasValue)
        {
            SetXmlAttributeValue(element, "width", Width);
        }

        if (Height.HasValue)
        {
            SetXmlAttributeValue(element, "height", Height);
        }
    }

    public override void ReadXml(XElement element)
    {
        base.ReadXml(element);

        Name = GetXmlAttributeValue<string>(element, "name");
        Caption = GetXmlAttributeValue<string>(element, "caption");
        Description = GetXmlAttributeValue<string>(element, "description");
        ShowCaption = GetXmlAttributeValue<bool?>(element, "show-caption");
        Width = GetXmlAttributeValue<int?>(element, "width");
        Height = GetXmlAttributeValue<int?>(element, "height");
    }
}
